package com.xjh.serverinspect.security.advice;

import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 请求参数解密过滤器
 * </p>
 *
 * @author xujinghui
 * @since 2024-12-20
 */
@Component
public class DecryptionFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        if ("POST".equals(request.getMethod()) && "application/x-www-form-urlencoded".equals(request.getContentType())) {
            // 使用自定义的HttpServletRequestWrapper替换原始请求
            chain.doFilter(new DecryptingHttpServletRequestWrapper(request), response);
        } else {
            chain.doFilter(request, response);
        }
    }


}
